/*
 * Copyright 2012 NJUT  qixiaobo. All rights reserved.
 */
package com.fujitsu.nanjing.tieba.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fujitsu.nanjing.tieba.base.BaseService;
import com.fujitsu.nanjing.tieba.constrant.CommonParamter;
import com.fujitsu.nanjing.tieba.ibator.CoreTopic;
import com.fujitsu.nanjing.tieba.ibator.CoreTopicCriteria;
import com.fujitsu.nanjing.tieba.model.Topic;

/**
 * TopicService.java
 * 
 * @author Qixiaobo-win8
 */
@Transactional
@Service
public class TopicService extends BaseService<CoreTopic, CoreTopicCriteria> {
	@Resource
	private Topic topic;
	@Override
	public CoreTopic findById(String id) {
		return topic.selectByPrimaryKey(Integer.parseInt(id));
	}

	@Override
	public CoreTopic insert(CoreTopic t) {
		topic.insert(t);
		return t;
	}

	@Override
	public void update(CoreTopic t) {
		topic.updateByPrimaryKeySelective(t);

	}

	@Override
	public void delete(CoreTopic t) {
		topic.deleteByPrimaryKey(t.getBoardid());

	}
	/** 根据boardid查找数据 */
	public List<CoreTopic> findAllByBoardId(int boardid, int offset,
			int length, String order) {
		CoreTopicCriteria example = new CoreTopicCriteria();
		example.setMysqlLength(length);
		example.setMysqlOffset(offset);
		example.setOrderByClause(order);
		example.createCriteria().andBoardidEqualTo(boardid);
		return topic.selectByExampleWithBLOBs(example);
	}

	/** 根据boardid查找数据,默认分页长度为10 */
	public List<CoreTopic> findAllByBoardIdDefault(int boardid, int offset) {
		CoreTopicCriteria example = new CoreTopicCriteria();
		example.setMysqlLength(CommonParamter.默认分页长度);
		if (offset > 0) {
			example.setMysqlOffset((offset - 1) * CommonParamter.默认分页长度);
		}
		example.setOrderByClause(CommonParamter.默认排序条件);
		example.createCriteria().andBoardidEqualTo(boardid);
		return topic.selectByExampleWithBLOBs(example);
	}

	public int countByBoardId(int boardId) {
		CoreTopicCriteria example = new CoreTopicCriteria();
		example.createCriteria().andBoardidEqualTo(boardId);
		return topic.countByExample(example);
	}

	public int count() {
		CoreTopicCriteria example = new CoreTopicCriteria();
		example.createCriteria().andBoardidIsNotNull();
		return topic.countByExample(example);
	}

}
